const db = uniCloud.database();
const dbCmd = db.command;
const dbJQL = uniCloud.databaseForJQL()
const dayjs = require('dayjs');


module.exports = {
	_before: function() { // 通用预处理器
		this.clientInfo = this.getClientInfo();
	},
	//新增用户
	async addUser(obj) {
		// 使用示例
		let maxNo = 1000
		// 获取到最大的
		let res = await db.collection('whjs_user').orderBy('num', 'desc')
			.limit(1)
			.get()
		if (res.data[0].num) {
			maxNo = res.data[0].num; // 获取最大编号
		}
		obj.num = maxNo + 1
		obj.clientIP = this.clientInfo.clientIP
		obj.createTime = new Date().getTime()
		// 收藏对象  selfCollage ，otherCollage
		obj.likObj = {}
		let resu = await db.collection('whjs_user').add(obj)
		return {
			data: {
				num: obj.num,
				resu
			}
		}
	},
	async updateUser(obj) {
		obj.updateTime = new Date().getTime()
		let resu = await db.collection('whjs_user').update(obj)
		return {
			data: {
				num: obj.num,
				resu
			}
		}
	},
	async getPlaceInfo(obj) {
		let {
			pageIndex,
			pageSize
		} = obj
		let res = await db.collection("whjs_sign_in").skip((pageIndex - 1) * pageSize)
			.limit(pageSize).orderBy("_id", "desc").get()
		console.log(res)
		// let userIds = res.data.map(item =>
		// 	item.userId)
		// console.log(res)
		// let userInfo = await db.collection("whjs_user").where({
		// 	userId: dbCmd.in(userIds)
		// }).get()
		return '' // userInfo
	},

	// 用户注册成功新增签到数据
	async addSignIn(userId) {
		let query = await db.collection("whjs_sign_in").where({
			userId
		}).get()
		if (query.data && query.data.length > 0) {
			return
		}
		let res = await db.collection("whjs_sign_in").add({
			userId: userId,
			signCount: 0
		})
		return res
	},

	//签到
	async signIn(userId) {
		let query = await db.collection("whjs_sign_in").where({
			userId
		}).get()
		let signCount = 0
		// if (!query.data || query.data.length == 0) {
		// 	return
		// };

		if (query.data.length > 0 && query.data[0].updateTime) {
			const otherDay = dayjs(query.data[0].updateTime); // 指定日期的开始时间
			if (otherDay.isSame(dayjs(), 'day')) {
				throw new Error("签到过了");
			} // 比较是否相同
			signCount = query.data[0].signCount
		}
		let res = await db.collection("whjs_sign_in").where({
			userId
		}).update({
			updateTime: new Date().getTime(),
			signCount: signCount + 1
		})

		return {
			data: {
				signCount: signCount + 1,
				res
			}
		}
		res
	},
	async getUserInfo(userId, type) {
		// 1\获取用户信息
		// 2获取签到信息
		// let res = await db.collection(userTem, signTem).get()
		let userInfo = await db.collection("whjs_user").where({
			userId
		}).get()
		if (type && type == 'onlyUser') {
			return userInfo
		}
		let signInfo = await db.collection("whjs_sign_in").where({
			userId
		}).get()
		let data = {
			userInfo: userInfo.data[0],
			signInfo: signInfo.data[0]
		}
		return data
	},
	async getSurf() {
		let res = await db.collection('whjs_user')
			.orderBy('createTime', "asc")
			.get()
		// .skip((pageIndex - 1) * pageSize)
		// .limit(pageSize)
		return res

	},
	async getLikeCollage(type, cloaageId, currentUserId) {
		let query = await db.collection("whjs_user").where({
			userId: currentUserId
		}).get()
		if (!query.data || query.data.length == 0) {
			throw new Error("请先登录");
		} else {
			let data = query.data[0]
			if (!data.likObj) {
				data.likObj = {
					selfCollage: [cloaageId]
				}
			} else {
				// 有值 没有收藏过
				if (data.likObj && data.likObj.selfCollage && !data.likObj.selfCollage.includes(cloaageId)) {
					data.likObj.selfCollage.push(cloaageId)
				} else if (data.likObj && data.likObj.selfCollage && data.likObj.selfCollage.length > 0 && data
					.likObj
					.selfCollage.includes(cloaageId)) {
					// 有值 收藏过

					data.likObj.selfCollage = data.likObj.selfCollage.filter(item => item !== cloaageId);
				}

			}
			delete data._id
			data.updateTime = new Date().getTime()
			// 先查出用户表中收藏 在操作
			let res = await db.collection("whjs_user").where({
				userId: currentUserId
			}).update(data)
			return res
		}
	},
	async getTopFiveFun() {

		let res = await db.collection("whjs_sign_in").skip(0)
			.limit(5).orderBy("signCount", "desc").get()
		let userIds = res.data.map(item =>
			item.userId)

		let userInfo = await db.collection("whjs_user").where({
			userId: dbCmd.in(userIds)
		}).get()

		return userInfo
	},
	_after(error, result) {
		if (error) {
			throw error
		}
		debugger
		// result.timeCost = Date.now() - this.startTime
		// result.author="咸虾米开发"
		result.success = true
		result.code = 200
		return result;
	},
}